package com.project.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.SellBean;
import com.project.mapper.ISellMapper;
import com.project.service.ISellService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
@Transactional
public class SellService implements ISellService {
    @Autowired
    private ISellMapper sellMapper;

    @Override
    public List<SellBean> findAll(Integer userId) {
        return sellMapper.findAll(userId);
    }

    @Override
    public void add(Integer userId, String goodsId) {
        QueryWrapper<SellBean> qw = new QueryWrapper<>();
        qw.eq("fk_userId",userId);
        qw.eq("fk_goodsId",goodsId);
        SellBean sellCar = sellMapper.selectOne(qw);
        if(sellCar==null){
            SellBean sellBean = new SellBean(1);
            sellBean.setFk_goodsId(goodsId);
            sellBean.setFk_userId(userId);
           sellMapper.insert(sellBean);
        }else {
            sellCar.setNum(sellCar.getNum()+1);
            sellMapper.updateById(sellCar);
        }
    }

    @Override
    public void deleteBySellId(Integer sellCarId) {
        sellMapper.deleteById(sellCarId);
    }

    @Override
    public void deleteByUserId(Integer userId) {
        QueryWrapper<SellBean> qw = new QueryWrapper<>();
        qw.eq("fk_userId",userId);
        sellMapper.delete(qw);
    }

    @Override
    public void out(String customName, String address, Integer total, Integer userId) {

    }
    @Override
    public void updateSellNum(Integer sellCarId, Integer newSellNum) {
        SellBean sellCar = sellMapper.selectById(sellCarId);
        sellCar.setNum(newSellNum);
        sellMapper.updateById(sellCar);
    }

}
